package mine.activity;

import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.orm.SugarRecord;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.text.ParseException;
import java.util.Calendar;

import mine.broadcast.AlarmReceiver;
import model.BeiWang;
import utils.TimeUtil;

/**
 * 添加备忘录
 * Created by Administrator on 2016/3/19.
 */
public class AddBeiWangActivity extends UIHandler.BaseActivity {

    View v_date;
    View v_time;
    Button btn_add;

    BeiWang beiWang = new BeiWang();

    String str_date;
    String str_time;

    TextView tv_date;
    TextView tv_time;

    EditText et_notify;

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_date)
            {
                pickDate();
            }
            else if (v == v_time)
            {
                pickTime();
            }
            else if (v == btn_add)
            {
                addBeiwang();
            }
        }
    };
    public static int AlarmIndex = 0;

    private void addBeiwang() {
        if (str_date == null)
        {
            Toast.makeText(MyApplication.share(),"日期不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (str_time == null)
        {
            Toast.makeText(MyApplication.share(),"时间不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (et_notify.getText().toString().isEmpty())
        {
            Toast.makeText(MyApplication.share(),"通知不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        String timeFinal = str_date+" "+str_time;
        beiWang = new BeiWang(timeFinal,et_notify.getText().toString());
        SugarRecord.save(beiWang);

        setAlarm();
        finish();

    }

    private void setAlarm() {
         /* 获取闹钟管理的实例 */

        String timeFinal = str_date+" "+str_time;
        long l = 0;
        try {
            l = TimeUtil.str2TimeStamp(timeFinal);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        AlarmManager mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
        Intent intent = new Intent(AddBeiWangActivity.this,
                AlarmReceiver.class);
        intent.putExtra("id",beiWang.getId());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                AddBeiWangActivity.this, beiWang.getId().intValue(), intent, 0);// 第二个参数为区别不同闹铃的唯一标识
                    /* 设置闹钟 */

        mAlarm.set(AlarmManager.RTC_WAKEUP, l, pendingIntent);
    }

    private void pickTime() {
        Calendar c=Calendar.getInstance();
        Dialog dialog=new TimePickerDialog(
                this,
                new TimePickerDialog.OnTimeSetListener(){
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        setTime(hourOfDay,minute);
                    }
                },
                c.get(Calendar.HOUR_OF_DAY),
                c.get(Calendar.MINUTE),
                false
        );
        dialog.show();
    }

    private void setTime(int hourOfDay, int minute) {
        String str = "";
        if (minute >=0 && minute <= 9)
        {
            str = "0"+minute;
        }
        else
        {
            str = minute+"";
        }
        str_time = hourOfDay+":"+str+":"+"00";
        tv_time.setText(hourOfDay + ":" + minute);
    }



    private void pickDate() {
        Calendar c = Calendar.getInstance();
        Dialog dialog = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
                        setDate(year,month,dayOfMonth);
                    }
                },
                c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        dialog.show();
    }

    private void setDate(int year, int month, int dayOfMonth) {
        String str = "";
        int i = month + 1;
        if (i >=0 && i <=9)
        {
            str = "0"+i;
        }
        else
        {
            str = i+"";
        }

        str_date = year+"/"+str+"/"+dayOfMonth;
        tv_date.setText(year + "/" + (month + 1) + "/" + dayOfMonth);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addbeiwang);
        initview();
    }

    private void initview() {
        v_date = findViewById(R.id.id_v_pickDate);
        v_date.setOnClickListener(onClickListener);

        v_time = findViewById(R.id.id_v_pickTime);
        v_time.setOnClickListener(onClickListener);

        btn_add = (Button) findViewById(R.id.id_btn_add);
        btn_add.setOnClickListener(onClickListener);

        tv_date = (TextView) findViewById(R.id.id_tv_date);
        tv_time = (TextView) findViewById(R.id.id_tv_time);

        et_notify = (EditText) findViewById(R.id.id_et_notify);

    }
}
